<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="LAFDC.DataAccess" %>
<%@ Import Namespace="LAFDC.Logging" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
   <title>Routage: Edition</title>
   <link type="text/css" rel="stylesheet" href="skins/style.css"/>
   <link type="text/css" rel="stylesheet" href="skins/forms.css"/>
   <script type="text/javascript">
       function CheckForm() {
        if (document.routage_form.asso.value!=asso){
            asso=document.routage_form.asso.value;
        }
        if(document.routage_form.code.value!=code){
            code=document.routage_form.code.value;
        }
        if(document.routage_form.destinataire.value!=destinataire){
            destinataire=document.routage_form.destinataire.value;
        }
        if(document.routage_form.address1.value!=address_1){
            address_1=document.routage_form.address1.value;
        }
        if(document.routage_form.address2.value!=address_2){
            address_2=document.routage_form.address2.value;
        }
        if(document.routage_form.ville.value!=ville_cp){
            ville_cp=document.routage_form.ville.value;
        }
        if(document.routage_form.quantite.value!=quantite){
            quantite=document.routage_form.quantite.value;
        }
        var save_url="routagesave.aspx?prev_id=" + prev_id + "&asso=" + asso + "&code=" + code + "&destinataire=" + destinataire + "&address_1=" + address_1 + "&address_2=" + address_2 + "&ville_cp=" + ville_cp+ "&quantite=" + quantite;
        
        // Redirect to the save page
        window.location=save_url;
    }
    </script>
   <script language="c#" runat="server">
        public static readonly string source = "JournalEdition.aspx.aspx";
        private string message = string.Empty;
        private string jrnl_previd = string.Empty;
        private string js_data = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (this.Request.Params.Count > 0)
                {
                    jrnl_previd = this.Request.QueryString.Get("adh");
                    message = "Requested to load journal reference prev_id = " + jrnl_previd + "<br/>";
                    
                    /* Check if the provided jrnl_previd exists in the DB */
                    message += "\n" + ExecuteSql(@"select * from routage where prev_id = '" + jrnl_previd + "'");
                }
            }
        }

        private string ExecuteSql(string sql)
        {
            ConnectionManager cn = new ConnectionManager();
            SqlConnection conn = cn.GetSqlConnection();
            SqlCommand tsql = conn.CreateCommand();

            tsql.CommandType = CommandType.Text;
            tsql.CommandText = sql;

            FDCLogger.LogEvent("Preparing to execute SQL statement: " + sql, source, 1);

            conn.Open();
            using (SqlDataReader reader = tsql.ExecuteReader())
            {
                while (reader.Read())
                {
                    if (reader.HasRows)
                    {
                        FDCLogger.LogEvent("La page est en train de charger les informations de routage", source, 4);
                        
                        string prev_id = string.Empty;
                        string asso = string.Empty;
                        string code = string.Empty;
                        string destinataire = string.Empty;
                        string address_1 = string.Empty;
                        string address_2 = string.Empty;
                        string ville_cp = string.Empty;
                        string quantite = string.Empty;
                        string synchrone = string.Empty;
                        
                        prev_id = reader.GetString(0);
                        asso = reader.GetValue(1).ToString();
                        code = reader.GetValue(2).ToString();
                        destinataire = reader.GetString(3);
                        address_1 = reader.GetString(4);
                        address_2 = reader.GetString(5);
                        ville_cp = reader.GetString(6);
                        quantite = reader.GetValue(7).ToString();
                        synchrone = reader.GetValue(8).ToString();
                        
                        FDCLogger.LogEvent("Result set = '" + prev_id + ";" + asso + ";" + code + ";" + destinataire + ";" + address_1 + ";"
                        + address_2 + ";" + ville_cp + ";" + quantite + ";" + synchrone + ";", source, 3);
                        
                        js_data="<script type=\"text/javascript\">var prev_id='" + prev_id + "'; var asso='" + asso + "'; var code='" + code + "';var destinataire='"+destinataire + "'; var address_1='"+address_1 + "';var address_2='"+address_2 + "';var ville_cp='"+ville_cp + "';var quantite='"+quantite+"';var synchrone='"+synchrone+"';</script";
                        js_data += ">";
                    }
                }
            }

            conn.Close();
            
            return "EXECUTED SQL @ " + DateTime.Now.ToString();
        }
</script>
</head>
<body>
    <div id="header">
        <table style="width: 100%">
            <tr style="height: 30px">
                <td style="width: 3%" align="center"><a href="/LAFDCWeb" target="_parent">
                    <img alt="Accueil" src="images/home_16x16.PNG" border="0" /></a></td>
                <td style="width: 70%">Fichier adh&eacute;rent: Routage du Journal de la Famille du Cheminot</td>
                <td style="width: 20%" align="right">
					<a href=""><img src="images/del.gif" border="0" alt="Annuler"/></a>
				</td>
            </tr>
        </table>
        <hr />   
    </div>
	<form name="routage_form">
		<label>Prev_id:</label> <input class="short" id="prev_id" readonly="readonly"/><br/>
		<div class="hidden"><label>Association:</label> <input class="short" id="asso"/><br/></div>
		<label>Code:</label> <input class="short" id="code"/><br/>
		<label>Destinataire:</label> <input id="destinataire" /><br/>
		<label>Adresse:</label> <input id="address1"/><br/>
		<label>Adresse:</label> <input id="address2"/><br/>
		<label>Code postal Ville:</label> <input id="ville"/><br/>
		<label>Quantite:</label> <input class="short" id="quantite"/><br/>
		<div class="hidden"><label>Synchrone:</label> <input id="synchrone" readonly="readonly"/><br/></div>
		<!--label id="debug_label"></label><br/-->
		<input type="button" id="submit" value="Enregistrer" onClick="CheckForm()"/> <input type="Button" ID="cancel" value="Annuler" onClick="window.location.reload()"/>
	</form>
</body>
<%Response.Write(js_data);%>
<script type="text/javascript">
    document.routage_form.prev_id.value=prev_id;
    document.routage_form.asso.value=asso;
    document.routage_form.code.value=code;
    document.routage_form.destinataire.value=destinataire;
    document.routage_form.address1.value=address_1;
    document.routage_form.address2.value=address_2;
    document.routage_form.ville.value=ville_cp;
    document.routage_form.quantite.value=quantite;
    document.routage_form.synchrone.value=synchrone;
    
</script>
</html>